package com.chinaedu.oc.action.common;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.chinaedu.oc.constant.Constant;
import com.chinaedu.oc.entity.common.Area;
import com.chinaedu.oc.entity.common.CodeList;
import com.chinaedu.oc.entity.common.Grade;
import com.chinaedu.oc.entity.common.Level;
import com.chinaedu.oc.entity.common.Specialty;
import com.chinaedu.oc.entity.common.System;
import com.chinaedu.oc.po.commodity.dto.CommodityListBaseDto;
import com.chinaedu.oc.po.productline.ProductLine;
import com.chinaedu.oc.po.productline.dto.QueryProductLineInput;
import com.chinaedu.oc.po.productline.dto.QueryProductLineOutput;
import com.chinaedu.oc.po.saledate.Saledate;
import com.chinaedu.oc.service.cache.ProductLineCacheServ;
import com.chinaedu.oc.service.cache.StaticCacheServ;
import com.chinaedu.oc.uri.IName;
import com.chinaedu.oc.util.common.StringUtil;
import com.chinaedu.oc.util.date.DateUtil;
import com.chinaedu.oc.util.http.HttpHelper;
import com.chinaedu.oc.util.http.HttpRequestException;
import com.chinaedu.oc.vo.Code;
import com.chinaedu.oc.vo.CommonListVo;

@Controller
@RequestMapping("/code")
public class DropDownListAction {

	@Autowired
	StaticCacheServ cacheServ;
	@Autowired
	ProductLineCacheServ productLineCacheServ;

	/**
	 * 获取地域
	 * @Description: TODO
	 * @param @return
	 * @return CommonListVo<Area>    返回类型
	 * @throws
	 */
	@RequestMapping("/area")
	@ResponseBody
	public CommonListVo<Area> getArea(){
		List<Area> list = cacheServ.query(Constant.AREA_DATA, Area.class);

		CommonListVo<Area> vo = new CommonListVo<>();
		vo.setData(list);
		vo.setStatus(Constant.TRUE);

		return vo;
	}

	/**
	 * 获取下拉框
	 * @Description: TODO
	 * @param @param type
	 * @param @param con
	 * @param @return
	 * @param @throws HttpRequestException
	 * @return CommonListVo<Code>    返回类型
	 * @throws
	 */
	@ResponseBody
	@RequestMapping("/list")
	public CommonListVo<Code> getCodeList(String type, String con) throws HttpRequestException{

//		String[] common = new String[] {
//							"business_type", "start_use", "department", "product_type",
//							"bill_type", "product_review_status"};
		String[] relation = new String[] {
				Constant.SCHOOL_LEVEL, Constant.SCHOOL_SYSTEM,Constant.SCHOOL_GRADE, Constant.SCHOOL_SPECIALTY
		};
		String[] business = new String[] {
				Constant.PRODUCT_LINE, Constant.SALEDATE
		};

		//List<String> commonList = Arrays.asList(common);
		List<String> relationList = Arrays.asList(relation);
		List<String> businessList = Arrays.asList(business);

		if(relationList.contains(type)){
//			List<Code> list = new ArrayList<>();
//			CommonListVo<Code> commo = new CommonListVo<>();
//			if(!StringUtil.isBlack(con)) {
//				String[] cons = con.split(",");
//				for(String c : cons) {
//					list.addAll(this.relation(type, c).getData());
//				}
//			}
//			commo.setData(list);
//			commo.setStatus(Constant.TRUE);
//			return commo;
			return this.relation(type, con);
		}else if(businessList.contains(type)) {
			return this.business(type, con);
		}else {
			return this.common(type);
		}

	}

	//处理codelist中的数据
	private CommonListVo<Code> common(String type){
		List<CodeList> codeList = cacheServ.query(type, CodeList.class);
		List<Code> list = new ArrayList<Code>();
		for(CodeList c : codeList) {
			list.add(new Code(c.getKey(), c.getValue()));
		}

		CommonListVo<Code> ddl = new CommonListVo<Code>();
		ddl.setStatus(Constant.TRUE);
		ddl.setData(list);

		return ddl;
	}
	//处理学制学段等的数据
	private CommonListVo<Code> relation(String type, String con){
		CommonListVo<Code> vo = new CommonListVo<Code>();
		vo.setStatus(Constant.TRUE);
		List<Code> listCode = new ArrayList<>();
		vo.setData(listCode);
		if(StringUtil.isBlack(con)){
			//学制
			if("school_system".equals(type)) {
				List<System> listSys = cacheServ.query(type, System.class);
				for(System s : listSys) {
					listCode.add(new Code(s.getId(), s.getSystemName()));
				}
				vo.setData(listCode);
				return vo;
			//学段
			}else if("school_level".equals(type)) {
				List<Level> listLevel = cacheServ.query(type, Level.class);
				for(Level l : listLevel) {
					listCode.add(new Code(l.getId(), l.getLevelName()));
				}
				vo.setData(listCode);
				return vo;
			}
		}else {
			String[] strs = con.split(",");
			for(String cons : strs) {
				//年级
				if("school_grade".equals(type)) {
					List<Grade> list = cacheServ.query(type,cons, Grade.class);
					for(Grade g : list) {
						listCode.add(new Code(g.getId(), g.getGradeName()));
					}
				//学科
				}else if("school_specialty".equals(type)) {
					List<Specialty> listSp = cacheServ.query(type, cons, Specialty.class);
					for(Specialty s : listSp) {
						listCode.add(new Code(s.getId(), s.getSpecialtyName()));
					}
				}
			}
			vo.setData(listCode);
			return vo;
		}

		return vo;
	}
	//跟业务相关的数据
	private CommonListVo<Code> business(String type, String con) throws HttpRequestException{

		//第一次缓存
		if(com.chinaedu.oc.context.Context.isCacheProductLine) {
			productLineCacheServ.addCache();
			com.chinaedu.oc.context.Context.isCacheProductLine = false;
		}

		CommonListVo<Code> ddl = new CommonListVo<Code>();
		ddl.setStatus(Constant.TRUE);
		ddl.setData(null);
		if("product_line".equals(type)) {
//			QueryProductLineInput input = new QueryProductLineInput();
//			QueryProductLineOutput out = HttpHelper.sendRequest(IName.ProductLine.QUERY_PRODUCT_LINE, input , QueryProductLineOutput.class);
//			List<ProductLine> list = out.getDataList();
//
//			List<Code> listCode = new ArrayList<Code>();
//			for(ProductLine pl : list) {
//				listCode.add(new Code(pl.getId(), pl.getName()));
//			}
//			CommonListVo<Code> ddl = new CommonListVo<Code>();
//			ddl.setStatus(Constant.TRUE);
//			ddl.setData(listCode);
//			return ddl;

			List<ProductLine> list = productLineCacheServ.getAllProductLine();

			List<Code> listCode = new ArrayList<Code>();
			for(ProductLine pl : list) {
				listCode.add(new Code(pl.getId(), pl.getName()));
			}
			ddl = new CommonListVo<Code>();
			ddl.setStatus(Constant.TRUE);
			ddl.setData(listCode);
			return ddl;
		} else if(Constant.SALEDATE.equals(type)){
			List<Code> listCode = new ArrayList<Code>();
			Map<String, String> map = new HashMap<String,String>();
			map.put("commodityId", con);
			 String json = HttpHelper.sendRequest(
		                IName.SaleDate.FIND_SALEDATE, map);
			 CommodityListBaseDto<Saledate> dto = JSON.parseObject(
	                 json, new TypeReference<CommodityListBaseDto<Saledate>>(){});
	        List<Saledate> saledates = dto.getData();
	        if(null != saledates && saledates.size() >0){
    			 for (Saledate saledate : saledates) {
    			     Date startDate = saledate.getStartDate();
    			     Date endDate = saledate.getEndDate();
    			     String beginDate = DateUtil.convertDate(startDate);
    			     String convertDate = DateUtil.convertDate(endDate);
    				 listCode.add(new Code(saledate.getId(),beginDate+"~"+convertDate));
    			}
	        }
			 ddl = new CommonListVo<Code>();
			 ddl.setData(listCode);
			 ddl.setStatus(Constant.TRUE);
			 return ddl;
		}
		return ddl;
	}

}
